package com.stbu.controller;

import com.stbu.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController {


    @RequestMapping("/toReg")
    public String toReg(){
        return "reg";
    }
    @RequestMapping("/reg")
    public String reg(@RequestParam("username") String username,
                      @RequestParam("pwd") String pwd, Model model){

        Date regDate = new Date();
        User user = new User();
        user.setUsername(username);
        user.setPassword(pwd);
        model.addAttribute("user", user);
        model.addAttribute("regDate", regDate);
        return "forward:toLogin";
    }


    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("username") String username, @RequestParam("pwd") String pwd,
                        @SessionAttribute("user")User user, ModelAndView mView){
        String msg="";
        if(username.equalsIgnoreCase(user.getUsername()) && pwd.equalsIgnoreCase(user.getPassword())){
            msg="登录成功";
        }else {
            msg="登录失败";
        }
        mView.addObject("msg", msg);
        mView.addObject("username", username);
        mView.setViewName("result");
        return mView;
    }

}
